<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/16 0016
 * Time: 17:18
 */
namespace app\admin\Controller;
use think\db;
use app\admin\model\Coach as Coachs;

use think\Controller;
class Coach extends Controller{
    //获取教练列表
    public function coachList(){
        return $this->fetch();
    }
    public function coachlistdata($page,$limit){
        $coach=Coachs::with('driver')
            ->where('is_del',0)
            ->page($page,$limit)
            ->select();
        $count=Db::name('coach')->count();
        foreach ($coach as $k=>$v){
            $coach[$k]['qingjia']=explode('&',$v['qingjia']);
            $coach[$k]['drivername']=$v->driver->name;
        }
        return json(['code'=>0,'msg'=>'返回成功','data'=>$coach,'count'=>$count]);
    }
    //添加教练
    public function coachAdd(){
        $driver=Db::name('driver_type')
            ->select();
        $this->assign('driver',$driver);
        return $this->fetch();
    }
    public  function coachAddForm(){
        $post=input('post.');
        $data['name']=$post['name'];
        $data['tel']=$post['tel'];
        $data['qingjia']=$post['qingjia'];
        $data['driver_id']=$post['driver_id'];
        $data['coach_image']=$post['coach_image'];
        $flag=Db::name('coach')
            ->insert($data);
        if($flag){
            return json(['code'=>1,'msg'=>'添加成功']);
        }
        return json(['code'=>0,'msg'=>'添加失败']);

    }
    //编辑教练
    public function coachEdit(){
        $id=input('get.id');
        //驾照类型
        $driver=Db::name('driver_type')
            ->select();
        //教练数据
        $coach=Db::name('coach')
            ->where('id',$id)
            ->find();
        $this->assign('data',$coach);
        $this->assign('driver',$driver);
        return $this->fetch();
    }
    public function coachEditForm(){
        $post=input('post.');
        $data['coach_image']=$post['coach_image'];
        $data['name']=$post['name'];
        $data['tel']=$post['tel'];
        $data['driver_id']=$post['driver_id'];
        $data['qingjia']=$post['qingjia'];
        $flag=Db::name('coach')
            ->where('id',$post['id'])
            ->update($data);
        if($flag){
            return json(['code'=>1,'msg'=>'修改数据成功']);
        }
        return json(['code'=>0,'msg'=>'修改数据失败']);

    }
    public function goodsImgDel(){
        $url = "../public".input('post.imgUrl');
        if (file_exists($url)){
            if (!unlink($url)){
                return json(['code'=>0,'msg'=>'删除失败']);

            }
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }
    public function shopImage(){
        $file =request()->file('file');
        $info = $file->move( '../public/uploads');
        if ($info) {
            $result = [
                'code'     => 1,
                'msg'      => '上传成功',
                'url' => '/uploads/' . str_replace('\\', '/', $info->getSaveName())
            ];
        } else {
            $result = [
                'code' => 0,
                'msg'  => $file->getError()
            ];
        }
        return $result;
    }
    //删除
    public function del(){
        $post=input('post.');
        $data['is_del']=1;
        $flag=Db::name('coach')
            ->where('id','in',$post['ids'])
            ->update($data);
        if($flag){
            return json(['code'=>1,'msg'=>'删除成功']);
        }
        return json(['code'=>0,'msg'=>'删除失败']);
    }
}